package my.merborn.ordersysteam105.models.servicesImpl;

import my.merborn.ordersysteam105.models.dao.UserMapper;
import my.merborn.ordersysteam105.models.pojo.User;
import my.merborn.ordersysteam105.models.pojo.enums.PayStatus;
import my.merborn.ordersysteam105.models.pojo.enums.Power;
import my.merborn.ordersysteam105.models.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServicesImpl implements UserService {

    @Autowired
    UserMapper um;

    @Override
    public List<User> getALL() {
        List<User> users = um.selectALL();
        return users;
    }

    @Override
    public User getUserById(Integer uid) {
        User user = um.selectByPrimaryKey(uid);
        return user;
    }

    @Override
    public boolean registUser(User user) {
        int i = 0;
        if (user.getuId() != null) {
            return false;
        }
        if (user.getuName() == null || user.getuPwd() == null) {
            return false;
        }
        try {
            i = um.insertSelective(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (i == 0) return false;
        else return true;
    }

    @Override
    public boolean updateUser(User user) {
        if (user.getuId() == null) return false;
        if (um.updateByPrimaryKeySelective(user) == 0) return false;
        else
            return true;
    }

    @Override
    public int deleteUserById(Integer uid) {
        return um.deleteByPrimaryKey(uid);
    }

    @Override
    public boolean userIsPayed(User user) {
        if (user.getuId() != null) {
            User u = um.selectByPrimaryKey(user.getuId());
            if (u == null) return false;
            Integer payStatus = u.getuIspayed();
            if (PayStatus.PAY.getStatus().equals(payStatus)) return true;
            else return false;
        } else
            return false;
    }

    @Override
    public Power getUserPower(User user) {
        if (user.getuId() != null) {
            User u = um.selectByPrimaryKey(user.getuId());
            if (u == null) return null;
            String power = u.getuPower();
            return Power.parsePower(power);
        } else
            return null;
    }

    @Override
    public boolean loginCheck(String userName, String pwd) {
        if (userName == null || "".equals(userName)) {
            return false;
        } else {
            User user = um.selectByName(userName);
            if(user == null) return false;
            return user.getuPwd().equals(pwd);
        }
    }


    @Override
    public boolean loginCheck(User user) {
        User u = um.selectByPrimaryKey(user.getuId());
        if(u == null){
            u = um.selectByName(user.getuName());
        }
        if(u == null)return false;
        if(u.getuPwd().equals(user.getuPwd())){
            return true;
        }
        return false;
    }
}
